package city.spring.modules.material.service.impl;

import city.spring.modules.material.entity.MaterialQuantityEntity;
import city.spring.modules.material.entity.MaterialSettingEntity;
import city.spring.modules.material.entity.StorehouseEntity;
import city.spring.modules.material.repository.MaterialQuantityRepository;
import city.spring.modules.material.service.MaterialQuantityService;
import city.spring.modules.material.service.MaterialSettingService;
import city.spring.modules.material.service.StorehouseService;
import city.spring.modules.material.vo.MaterialQuantityVO;
import city.spring.modules.material.vo.PrintMaterialQuantityVO;
import city.spring.utils.UserUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service：物资库存信息表
 *
 * @author 谢金婷
 * @date 2020-12-15 16:06:31
 */
@CacheConfig(cacheNames = {MaterialQuantityServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class MaterialQuantityServiceImpl extends ServiceImpl<MaterialQuantityRepository, MaterialQuantityEntity> implements MaterialQuantityService {

    private final MaterialQuantityRepository materialQuantityRepository;
    private final StorehouseService storehouseService;
    private final MaterialSettingService materialSettingService;

    public MaterialQuantityServiceImpl(MaterialQuantityRepository materialQuantityRepository, StorehouseService storehouseService, MaterialSettingService materialSettingService) {
        this.materialQuantityRepository = materialQuantityRepository;
        this.storehouseService = storehouseService;
        this.materialSettingService = materialSettingService;
    }

    @Override
    public void saveMaterialQuantity(MaterialQuantityEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMaterialQuantity(MaterialQuantityEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMaterialQuantity(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMaterialQuantity(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public MaterialQuantityEntity getByMaterialIdAndStorehouseId(String materialId, String storehouseId) {
        return materialQuantityRepository.getByMaterialIdAndStorehouseId(materialId, storehouseId);
    }

    @Override
    public IPage<MaterialQuantityVO> listPage(Page<? extends MaterialQuantityEntity> page, Wrapper<? extends MaterialQuantityEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

    @Override
    public List<MaterialQuantityVO> getMaterialByStorehouseId(String storehouseId) {
        return materialQuantityRepository.getMaterialByStorehouseId(storehouseId);
    }

    @Override
    public MaterialQuantityEntity getByMaterialId(String materialId) {
        return materialQuantityRepository.getByMaterialId(materialId);
    }

    @Override
    public PrintMaterialQuantityVO getPrintMaterialQuantity(List<String> ids) {
        List<MaterialQuantityEntity> list;
        if (ids.isEmpty()) {
            list = new ArrayList<>();
        } else {
            list = lambdaQuery().in(MaterialQuantityEntity::getId, ids).list();
        }
        PrintMaterialQuantityVO vo = new PrintMaterialQuantityVO();
        vo.setPreparer(UserUtils.getUser().getNickname());
        // 生成打印时所需要的打印明细
        List<PrintMaterialQuantityVO.PrintMaterialQuantityRecordVO> recordVOList = list.stream().map(item -> {
            StorehouseEntity storehouseEntity = storehouseService.getById(item.getStorehouseId());
            MaterialSettingEntity materialSettingEntity = materialSettingService.getById(item.getMaterialId());
            PrintMaterialQuantityVO.PrintMaterialQuantityRecordVO recordVO = new PrintMaterialQuantityVO.PrintMaterialQuantityRecordVO();
            recordVO.setId(item.getId());
            recordVO.setStorehouseId(item.getStorehouseId());
            recordVO.setSname(storehouseEntity.getSname());
            recordVO.setMaterialId(item.getMaterialId());
            recordVO.setName(materialSettingEntity.getName());
            recordVO.setCode(item.getCode());
            recordVO.setUnitmeasurement(item.getUnitmeasurement());
            recordVO.setUnitprice(item.getUnitprice());
            recordVO.setWarehousingquantity(item.getWarehousingquantity());
            recordVO.setDeliveryquantity(item.getDeliveryquantity());
            recordVO.setWarehousingInventoryAmount(item.getWarehousingInventoryAmount());
            recordVO.setDeliveryInventoryAmount(item.getDeliveryInventoryAmount());
            recordVO.setQuantity(item.getQuantity());
            recordVO.setTotal(item.getTotal());
            return recordVO;
        }).collect(Collectors.toList());
        vo.setList(recordVOList);
        return vo;
    }

    @Override
    public IPage<MaterialQuantityVO> materialByStorehouseIdlistPage(Page<? extends MaterialQuantityEntity> page, Wrapper<? extends MaterialQuantityEntity> wrapper, String storehouseId) {
        return baseMapper.MaterialByStorehouseIdlistPage(page, wrapper, storehouseId);
    }

    @Override
    public IPage<MaterialQuantityVO> selectlistPage(Page<? extends MaterialQuantityEntity> page, Wrapper<? extends MaterialQuantityEntity> wrapper) {
        return baseMapper.selectlistPage(page, wrapper);
    }

}